package com.wuhao.code.check.model

import com.aegis.mybatis.xmlless.model.base.BaseSimpleClassWrapper
import com.intellij.psi.PsiClass
import com.wuhao.code.check.actualType
import com.wuhao.code.check.model.base.BasePsiFieldWrapper
import com.wuhao.code.check.resolveKtClass
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtTypeAlias

/**
 *
 * @author 吴昊
 * @date 2024/1/19 12:08
 * @since v0.0.0
 * @version 1.0
 */
class KtTableFieldWrapper(private val property: KtDeclaration) : BasePsiFieldWrapper(property) {


  override fun getFieldType(): BaseSimpleClassWrapper {
    if (property is KtCallableDeclaration) {
      when (val ref = property.typeReference?.typeElement?.actualType?.resolveKtClass()) {
        is KtClass     -> {
          return SimpleKtClassWrapper(ref)
        }

        is PsiClass    -> {
          return SimplePsiClassWrapper(ref)
        }

        is KtTypeAlias -> {
          val realType = ref.getTypeReference()?.typeElement?.actualType?.resolveKtClass()
          if (realType is KtClass) {
            return SimpleKtClassWrapper(realType)
          } else if (realType is PsiClass) {
            return SimplePsiClassWrapper(realType)
          }
        }
      }
    }
    throw RuntimeException("无法获取字段类型")
  }

  override fun getFieldName(): String {
    return property.name ?: ""
  }


  override fun isCharSequence(): Boolean {
    return if (property is KtCallableDeclaration) {
      property.typeReference?.text in listOf("String", "String?")
    } else {
      false
    }
  }

}
